﻿using Desktop.Interfaces;
using Desktop.Library.HtmlToXaml;
using Google.GData.Client;
using System;
using System.Runtime.Serialization;
using System.Security;
using System.Threading;

namespace Desktop.Library
{
    [Serializable()]
    public class Story : ContentItem<Story>, System.ComponentModel.INotifyPropertyChanged
    {
        #region Variables

        private string _title;
        private string _author;
        private string _summary;
        private ObservableList<IContentItem> _contentItems;

        #endregion

        #region Constructors

        public Story()
            : base()
        {
            Title = string.Empty;
            Author = string.Empty;
            Summary = string.Empty;
            ContentItems = new ObservableList<IContentItem>();
        }

        public Story(Story story)
            : base(story)
        {
            if (story == null)
            {
                return;
            }
            Title = story.Title;
            Author = story.Author;
            Summary = story.Summary;
            ContentItems = story.ContentItems.Clone();
        }

        /// <summary>
        /// Import a blog from Blogger.
        /// </summary>
        /// <param name="feedUrl">The feed url, in the form of http://blogname.blogspot.com/feeds/posts/default</param>
        public Story(string feedUrl)
            : this()
        {
            ImportBlog(feedUrl);
        }

        public Story(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            Title = info.GetString("Title");
            Author = info.GetString("Author");
            Summary = info.GetString("Summary");
            ContentItems = info.GetValue("ContentItems", typeof(ObservableList<IContentItem>)) as ObservableList<IContentItem>;
        }

        #endregion

        #region Properties

        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                if (_title != value)
                {
                    PropertyHasChanged("Title");
                }
                _title = value;
            }
        }

        public string Author
        {
            get
            {
                return _author;
            }
            set
            {
                if (_author != value)
                {
                    PropertyHasChanged("Author");

                }
                _author = value;
            }
        }

        public string Summary
        {
            get
            {
                return _summary;
            }
            set
            {
                if (_summary != value)
                {
                    PropertyHasChanged("Summary");

                }
                _summary = value;
            }
        }

        public ObservableList<IContentItem> ContentItems
        {
            get
            {
                return _contentItems;
            }
            private set
            {
                if (_contentItems != value)
                {
                    PropertyHasChanged("ContentItems");

                }
                _contentItems = value;
            }
        }

        #endregion

        #region Methods

        public override string ToString()
        {
            return Title;
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Title", Title);
            info.AddValue("Author", Author);
            info.AddValue("Summary", Summary);
            info.AddValue("ContentItems", ContentItems);
            base.GetObjectData(info, context);
        }

        private void ImportBlog(string feedUrl)
        {
            if (!Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                throw new SecurityException("You must log in to use Google-specific features.");
            }
            GoogleIdentity ident = Thread.CurrentPrincipal.Identity as GoogleIdentity;

            Service service = new Service("blogger", "Desktop");
            service.Credentials = ident.Credentials;
            FeedQuery query = new FeedQuery(feedUrl);

            AtomFeed feed = service.Query(query);

            Title = feed.Title.Text;
            if (feed.Authors.Count > 0)
            {
                Author = feed.Authors[0].Name;
            }
            if (feed.Subtitle != null)
            {
                Summary = feed.Subtitle.Text;
            }

            while ((feed != null) && (feed.Entries.Count > 0))
            {
                foreach (AtomEntry entry in feed.Entries)
                {
                    Chapter chapter = new Chapter();
                    chapter.Title = entry.Title.Text;
                    chapter.Summary = entry.Summary.Text;
                    chapter.CreatedDate = entry.Published;

                    Section section = new Section();
                    section.Text = HtmlToXamlConverter.ConvertHtmlToXaml(entry.Content.Content, true);
                    chapter.Sections.Add(section);
                    
                    ContentItems.Add(chapter);
                }

                // Do the chunking:
                if (feed.NextChunk != null)
                {
                    query.Uri = new Uri(feed.NextChunk);
                    feed = service.Query(query);
                }
                else
                {
                    feed = null;
                }
            }
        }

        #endregion
    }
}